﻿using IShoppingBll;
using IShoppingDal;
using ShoppingEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingBll
{
    public class CommodityTypeBll : BaseBll<CommodityType>, ICommodityTypeBll
    {
        private ICommodityTypeDal _commodityTypeDal;

        public CommodityTypeBll(ICommodityTypeDal commodityTypeDal) : base(commodityTypeDal)
        {
            _commodityTypeDal = commodityTypeDal;
        }

        /// <summary>
        /// 商品类被下拉选
        /// </summary>
        /// <returns></returns>
        public object GetCommodityTypeOption()
        {
            var commodityType = _commodityTypeDal.GetEntitiesDb().Select(d => new
            {
                d.ID,
                d.CategoryName
            }).ToList();
            return commodityType;
        }

        /// <summary>
        /// 商品类型分类页面展示、模糊查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="CategoryName"></param>
        /// <returns></returns>
        public object GetCommodityTypePageList(int page, int limit, out int count, string CategoryName)
        {
            //获取数据库中类别全部没删除的数据（未真实查询）
            var commodityType = _commodityTypeDal.GetEntitiesDb().AsQueryable();

            if (!string.IsNullOrEmpty(CategoryName))
            {
                commodityType = commodityType.Where(c => c.CategoryName.Contains(CategoryName));
            }

            //查询出来数据的数量
            count = _commodityTypeDal.GetEntitiesDb().Count();

            //分页
            commodityType = commodityType.OrderBy(c => c.CategoryName).Skip((page - 1) * limit).Take(limit);

            var list = commodityType.ToList().Select(c =>
            {
                return new
                {
                    c.CategoryName,
                    c.ID,
                };
            });

            return list;
        }


        /// <summary>
        /// 判断是否有相同的商品类型名称
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public bool IsHadCommodityType(string categoryName)
        {
            return _commodityTypeDal.GetEntitiesDb().Where(c => c.CategoryName == categoryName).Count() > 0;
        }



    }
}
